package com.xuxiao.controller;

import com.xuxiao.common.Result;
import com.xuxiao.common.ReturnNameAndCardID;
import com.xuxiao.common.TransCord;
import com.xuxiao.entrty.TransInfo;
import com.xuxiao.service.TransInfoService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/trans")
public class TransInfoController {
    @Autowired
    private TransInfoService transInfoService;

    @PostMapping
    public Result addTransInfo(@RequestBody ReturnNameAndCardID returnNameAndCardID) {
        log.info("新增记录：{}", returnNameAndCardID);
        transInfoService.addTransInfo(returnNameAndCardID);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteByID(@PathVariable String id) {
        log.info("删除记录，{}", id);
        transInfoService.deleteByID(id);
        return Result.success("删除成功");
    }

    @GetMapping()
    public Result SelectAll() {
        log.info("查询所有交易记录");
        List<TransCord> transCordList = transInfoService.SelectAll();
        return Result.success(transCordList);
    }

    @PostMapping("/{id}")
    public Result SelectByID(@PathVariable String id) {
        log.info("按id查找记录，{}", id);
        List<TransCord> transCordList = transInfoService.SelectByID(id);
        return Result.success(transCordList);
    }

    @GetMapping("/{name}")
    public Result SelectByName(@PathVariable String name) {
        log.info("按姓名查找记录，{}", name);
        List<TransCord> transCordList = transInfoService.SelectByname(name);
        return Result.success(transCordList);
    }

    @GetMapping("/income/{name}")
    public Result Income(@PathVariable String name) {
        log.info("查询收入记录：{}", name);
        List<TransCord> transCordList = transInfoService.SelectIncome(name);
        return Result.success(transCordList);
    }
}
